/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import negocio.Tabela;


/**
 *
 * @author v3550
 */
public class FormPartner extends javax.swing.JInternalFrame implements ActionListener {

    /**
     * Creates new form FormPartner
     */
    private int newNodeSuffix = 1;
    private static String ADD_COMMAND = "Add";
    private static String REMOVE_COMMAND = "Remove";
    private static String CLEAR_COMMAND = "Clear";
    public ModelTree treePanel;

    Controlador controlador = null;

    public FormPartner() {
        initComponents();
        
        controlador = Controlador.getInstance();
        treePanel = new ModelTree(this);
        try {
            populateTree(treePanel);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "FormPartner :" + ex.getMessage());
        }
        treePanel.setBounds(0, 0, 280, 500);
        desktopTree.add(treePanel);
    }

    public void setDesktopTable(Component dp) {
        if (this.desktopTables.getSelectedFrame() != null) {
            this.desktopTables.removeAll();
        }
        this.desktopTables.add(dp);
    }

    public Dimension getDimensionDesktopTable() {
        return this.desktopTables.getSize();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        desktopTree = new javax.swing.JDesktopPane();
        desktopTables = new javax.swing.JDesktopPane();

        setClosable(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(desktopTree, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(desktopTables, javax.swing.GroupLayout.DEFAULT_SIZE, 697, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(desktopTree, javax.swing.GroupLayout.DEFAULT_SIZE, 588, Short.MAX_VALUE)
            .addComponent(desktopTables)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JDesktopPane desktopTables;
    private javax.swing.JDesktopPane desktopTree;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();

        if (ADD_COMMAND.equals(command)) {
            // Add button clicked
            treePanel.addObject("New Node " + newNodeSuffix++);
        } else if (REMOVE_COMMAND.equals(command)) {
            // Remove button clicked
            treePanel.removeCurrentNode();
        } else if (CLEAR_COMMAND.equals(command)) {
            // Clear button clicked.
            treePanel.clear();
        }
    }

    private void populateTree(ModelTree treePanel) throws Exception{
        DefaultMutableTreeNode dmtn = null;
        for (String n : controlador.getListOwners()) {
            String parent = new String(n);
            dmtn = treePanel.addObject(null, parent);
            for (Tabela t : controlador.getTabelas(n) ) {
                treePanel.addObject(dmtn, new String(t.getTabela()));
            }
        }
    }
    
   

}